library http_request;

import 'package:dio/adapter.dart';
import 'package:dio/dio.dart';
import 'package:dio/native_imp.dart';
import 'dart:io';

class Request extends DioForNative {
  Request([BaseOptions options]) : super(options);
}

class HttpRequest<T> {
  String baseUrl;
  String proxy;
  Request _request;
  HttpRequest({this.baseUrl = '', Interceptor interceptor}) {
    _request = Request(BaseOptions(baseUrl: baseUrl));
    if (getProxy() != null) {
      (_request.httpClientAdapter as DefaultHttpClientAdapter)
          .onHttpClientCreate = (client) {
        client.badCertificateCallback = (cert, host, port) {
          return Platform.isAndroid;
        };
        client.findProxy = (url) {
          ///设置代理 电脑ip地址
          return getProxy();
        };
      };
    }

    if (interceptor != null) {
      _request.interceptors.add(interceptor);
    }
  }

  /*
  get 请求
   */
  _Request<T> get(String url, {Options options}) {
    Options opts = options ?? Options();
    opts = opts.merge(
      method: 'GET',
    );
    return _Request<T>(url, request: _request, options: opts);
  }

  /*
  post请求
   */
  _Request<T> post(String url, {Options options}) {
    Options opts = options ?? Options();
    opts = opts.merge(method: 'POST');
    return _Request<T>(url, request: _request, options: opts);
  }

  _Request<T> downloadRequest(String url, String savePath, {Options options}) {
    return _Request<T>(url, request: _request, downloadPath: savePath);
  }

  // 添加拦截器
  void addInterceptors(Interceptor interceptor) {
    _request.interceptors.add(interceptor);
  }

  // 锁住后面请求
  void lock() {
    _request.lock();
  }

  // 解开请求锁
  void unlock() {
    _request.unlock();
  }

  String getProxy() {
    return proxy;
  }

  void setProxy(proxy) {
    this.proxy = proxy;
  }
}

/*
请求执行类
 */
class _Request<T> {
  String _url;
  Options _options;
  CancelToken _cancelToken;
  dynamic _requestParams;
  Request _request;
  String _downloadPath;

  _Request(this._url, {options, Request request, String downloadPath}) {
    this._options = options;
    this._request = request;
    this._downloadPath = downloadPath;
  }

  /*
  以函数方式调用，设置请求参数
   */
  _Request<T> call([Map<String, dynamic> data]) {
    _requestParams = data;
    return this;
  }

  /*
  执行请求
   */
  Future<T> execute(
      {ProgressCallback onSendProgress,
      ProgressCallback onReceiveProgress}) async {
    _cancelToken = CancelToken();
    var options = this._options ?? Options(method: 'GET');
    bool isGet = options.method.toUpperCase() == 'GET';

    var res = await _request.request<T>(this._url,
        data: isGet ? null : _requestParams,
        queryParameters: isGet ? _requestParams : null,
        cancelToken: _cancelToken,
        options: options,
        onReceiveProgress: onReceiveProgress,
        onSendProgress: onSendProgress);

    return res.data;
  }

  /*
  取消请求
   */
  void cancel({dynamic reason}) {
    if (_cancelToken != null) {
      _cancelToken.cancel(reason);
    }
  }

  /*
  下载文件
   */
  Future<dynamic> downloadFile({ProgressCallback onReceiveProgress}) async {
    _cancelToken = CancelToken();
    var res = await _request.download(
      this._url,
      this._downloadPath,
      data: _requestParams,
      cancelToken: _cancelToken,
      options: _options,
      onReceiveProgress: onReceiveProgress,
    );
    return res.data;
  }
}
